
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>发表留言</title>
  <link rel="stylesheet" href="/node_modules/bootstrap/dist/css/bootstrap.css">
</head>
<body>
  <div >
    <h2 class="sub-header" style="text-align: center; margin: 20px 0;">填写内容</h2>
    <section class="container" >
      <div class="row" >
        <div class="col-md-3"></div>
        <div class="col-md-6">
          <form action="/topics/new" method="POST" id="topic_form">
            <div class="form-group">
              <label for="exampleInputEmail1">选择主题</label>
              <select class="form-control" name="theme" id="theme">
                <option>分享</option>
                <option>问答</option>
                <option>招聘</option>
                <option>客户端测试</option>
              </select>
            </div>
            <div class="form-group">
              <label for="exampleInputEmail1">标题</label>
              <input type="text" class="form-control" id="exampleInputEmail1" placeholder="请输入标题" name="title">
            </div>
            <div class="form-group">
              <label for="exampleInputPassword1">内容</label>
              <textarea class="form-control" rows="3" name="content" id="content"></textarea>
            </div>
            <div class="form-group" style="display: none;">
              <label for="">发起人id</label>
              <input type="text" name="publisher_id" value="{{user._id}}" readonly style="border: none;">
            </div>
            <div class="form-group" style="display: none;">
              <label for="">发起人</label>
              <input type="text" name="publisher" value="{{user.nickname}}" readonly style="border: none;">
            </div>
            <div class="form-group" style="display: none;">
              <label for="">发表时间</label>
              <input type="text" name="createtime" value="getDate()" readonly style="border: none;" id="createtime">
            </div>
            <div class="form-group" style="display: none;">
              <label for="">发起人头像</label>
              <input type="text" name="publisher_avatar" value="{{user.avatar}}" readonly style="border: none;" id="publisher_avatar">
            </div>
            <button type="submit" class="btn btn-primary">Submit</button>
          </form>
        </div>
        <div class="col-md-3"></div>
      </div>
    </section>
  </div>
  <script src="/node_modules/jquery/dist/jquery.js"></script>
  <script src="/node_modules/moment/moment.js"></script>
  <script>
    var nowDate = new Date();
    nowDate = moment(nowDate).format('YYYY-MM-DD HH:mm:ss');
    document.getElementById('createtime').value = nowDate;

    $('#topic_form').on('submit', function(e) {
      e.preventDefault()
      let formData = $(this).serialize()
      console.log(formData);
      $.ajax({
        type: 'post',
        url: '/topics/new',
        dataType: 'json',
        data: formData,
        success: function(data) {
          var err_code = data.err_code
          if(err_code === 0) {
            window.alert('留言成功')
            window.location.href = '/'
          }else {
            window.alert('服务器正忙，请稍后再试')
          }
        }
      })
    })
  </script>
</body>
</html>

